package zookeeper;/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;

import java.util.concurrent.CountDownLatch;

public class EmbeddedZooKeeperMain {

    public static void main(String[] args) throws Exception {
        new EmbeddedZooKeeper(2181, false).start();

        // 获取服务注册中心工厂
        RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
        // 根据地址获取zookeeper注册中心的客户端实例
        Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://127.0.0.1:2181"));

        // 降级策略 force：强制  fail：会正常调用，失败了才降级
        String type = "force";
//        type = "fail";

        // 定义一个返回结果为null的降级方案：需要指明对哪个接口、分组、版本进行降级；category必须为configurators；
        // override://0.0.0.0/标识该降级策略对所有的服务消费者有效
        URL url = URL.valueOf(String.format("override://0.0.0.0/pwd.allen.service.IHelloService?category=configurators" +
                "&dynamic=false&application=service-provider&mock=%s:return+null&group=group1&version=pwd-1.0", type));

        // 注册
        registry.register(url);

        // 取消注册
//        registry.unregister(url);

        System.out.println("dubbo service started");
        new CountDownLatch(1).await();
    }
}
